package com.hfzy.ihk.web.customer;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.*;

/**
 * Created by Administrator on 2018/10/27.
 *
 * @author wws
 */
// 演示例子，执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
public class CodeGenerator {

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
//        String projectPath = System.getProperty("user.dir");
//        gc.setOutputDir(projectPath + "/web-demo/src/main/java");
        gc.setOutputDir("E:\\work\\hfzy_project\\hfzy-linkcall\\web-user-center\\src\\main\\java");
        gc.setAuthor("wws");
        gc.setOpen(false);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://172.16.8.84:3306/usercenter?useUnicode\\=true&characterEncoding\\=utf-8&allowMultiQueries\\=true&useSSL=true");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("cloudcall");
        dsc.setPassword("mydbacloudcall");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
//        pc.setModuleName(scanner("模块名"));
        pc.setModuleName("userCenter");//并非真的模块名，而是为了与下面参数拼com.hfzy.ihk.web.userCenter
        pc.setParent("com.hfzy.ihk.web");

        /*虽然能自定义生成路径，但是package不对，不行*/
//        setPathInfo(pathInfo, template.getMapper(), outputDir, ConstVal.MAPPER_PATH, ConstVal.MAPPER);
//        setPathInfo(pathInfo, template.getXml(), outputDir, ConstVal.XML_PATH, ConstVal.XML);
//        setPathInfo(pathInfo, template.getService(), outputDir, ConstVal.SERVICE_PATH, ConstVal.SERVICE);
//        setPathInfo(pathInfo, template.getServiceImpl(), outputDir, ConstVal.SERVICE_IMPL_PATH, ConstVal.SERVICE_IMPL);
        /*Map<String,String> pathInfo = new HashMap<>();
        pathInfo.put(ConstVal.ENTITY_PATH,"E:\\work\\hfzy_project\\hfzy-linkcall\\facade-user-center\\src\\main\\java\\com\\hfzy\\ihk\\facade\\userCenter\\entity");
        pathInfo.put(ConstVal.MAPPER_PATH,"E:\\work\\hfzy_project\\hfzy-linkcall\\web-user-center\\src\\main\\java\\com\\hfzy\\ihk\\web\\userCenter\\dao");
        pathInfo.put(ConstVal.SERVICE_PATH,"E:\\work\\hfzy_project\\hfzy-linkcall\\facade-user-center\\src\\main\\java\\com\\hfzy\\ihk\\facade\\userCenter\\service");
        pathInfo.put(ConstVal.SERVICE_IMPL_PATH,"E:\\work\\hfzy_project\\hfzy-linkcall\\web-user-center\\src\\main\\java\\com\\hfzy\\ihk\\web\\userCenter\\serviceImpl");
        pathInfo.put(ConstVal.XML_PATH,"E:\\work\\hfzy_project\\hfzy-linkcall\\web-user-center\\src\\main\\resources\\mybatis\\xml");
        pathInfo.put(ConstVal.CONTROLLER_PATH,"E:\\work\\hfzy_project\\hfzy-linkcall\\web-user-center\\src\\main\\java\\com\\hfzy\\ihk\\web\\userCenter\\controller");
        pc.setPathInfo(pathInfo);*/
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
//                return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
                return "E:\\work\\hfzy_project\\hfzy-linkcall\\web-user-center\\src\\main\\resources\\mybatis\\xml"
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
//        // 关闭默认 xml 生成，调整生成 至 根目录
        mpg.setTemplate(new TemplateConfig().setXml(null));

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setSuperEntityClass("com.hfzy.ihk.common.facade.BaseEntity");
        strategy.setEntityLombokModel(true);
//        strategy.setRestControllerStyle(true);
//        strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
//        strategy.setInclude(scanner("表名"));
//        strategy.setSuperEntityColumns("id");
//        strategy.setControllerMappingHyphenStyle(true);
//        strategy.setTablePrefix(pc.getModuleName() + "_");
        strategy.setInclude(new String[]{
                "role",
                "user",
                "priv",
                "resource",
                "user_has_role",
                "user_has_priv",
                "user_has_resource",
                "role_has_priv",
                "priv_has_resource",
                "log",
                "code_detail"
        });
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

}